<template>
  <div class="certificate-query">
    <div class="search-box">
      <h2>证书查询</h2>
      <div class="search-input">
        <input
          v-model="searchText"
          type="text"
          placeholder="请输入证书编号或条码"
          @keyup.enter="handleSearch"
        />
        <button :disabled="loading" @click="handleSearch">
          {{ loading ? '查询中...' : '查询' }}
        </button>
      </div>
      <div v-if="error" class="error-message">{{ error }}</div>
    </div>

    <div v-if="certificate" class="certificate-result">
      <h3>证书信息</h3>
      <div class="info-item">
        <span class="label">证书单位：</span>
        <span>{{ certificate.org_name }}</span>
      </div>
      <div class="info-item">
        <span class="label">证书编号：</span>
        <span>{{ certificate.cert_number }}</span>
      </div>
      <div class="info-item">
        <span class="label">证书条码：</span>
        <span>{{ certificate.barcode }}</span>
      </div>
      <div v-for="field in certificate.fields" :key="field.category_name" class="info-item">
        <span class="label">{{ field.category_name }}：</span>
        <span>{{ field.content }}</span>
      </div>
    </div>

    <div class="admin-link">
      <router-link to="/admin/login">管理入口</router-link>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { queryCertificate } from '@/api/certificateApi'

export default {
  name: 'CertificateQuery',
  setup() {
    const searchText = ref('')
    const certificate = ref(null)
    const error = ref('')
    const loading = ref(false)

    const handleSearch = async () => {
      if (!searchText.value) {
        error.value = '请输入证书编号或条码'
        return
      }

      try {
        loading.value = true
        error.value = ''
        certificate.value = await queryCertificate(searchText.value)
      } catch (err) {
        error.value = err.message
        certificate.value = null
      } finally {
        loading.value = false
      }
    }

    return {
      searchText,
      certificate,
      error,
      loading,
      handleSearch
    }
  }
}
</script>

<style lang="scss" scoped>
.certificate-query {
  max-width: 800px;
  margin: 20px auto;
  padding: 15px;
}

.search-box {
  text-align: center;
  margin-bottom: 30px;
}

.search-box h2 {
  font-size: 1.5rem;
  margin-bottom: 20px;
}

.search-input {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 500px;
  margin: 20px auto;
}

input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

button {
  width: 100%;
  padding: 12px;
  background: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;

  &:disabled {
    background: #a0cfff;
    cursor: not-allowed;
  }
}

.error-message {
  color: #f56c6c;
  margin-top: 10px;
}

.certificate-result {
  background: white;
  padding: 15px;
  border-radius: 4px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  margin-top: 20px;
}

.certificate-result h3 {
  font-size: 1.2rem;
  margin-bottom: 15px;
  color: #303133;
}

.info-item {
  margin: 12px 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.label {
  color: #606266;
  font-weight: bold;
}

.admin-link {
  text-align: center;
  margin-top: 30px;
  padding: 20px;
}

.admin-link a {
  color: #909399;
  text-decoration: none;
  font-size: 14px;
}

@media (min-width: 768px) {
  .search-input {
    flex-direction: row;
  }

  button {
    width: auto;
  }

  .info-item {
    flex-direction: row;
  }

  .label {
    width: 120px;
  }
}
</style>
